package com.pet.service;

import com.pet.bean.*;
import com.pet.dao.AccountDao;
import com.pet.dao.OwnerDao;
import com.pet.dao.PetDao;
import com.pet.dao.PetstoreDao;
import com.pet.util.DataSourcesTool;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class OwnerService {
    // 创建dao的对象
    OwnerDao ownerDao = new OwnerDao();
    PetDao petDao = new PetDao();
    AccountDao accountDao = new AccountDao();
    PetstoreDao petStoreDao = new PetstoreDao();
    //登录
    public Notification loginOwner(Notification notification){
        //提供数据库链接
        Connection con = DataSourcesTool.getConnection();
        //获取用户名和密码
        String loginName = (String) notification.getMap().get("loginName");
        String pass = (String) notification.getMap().get("pw");

        try {
            //1.调用数据库的操作方法来进行登录校验
            Owner owner = ownerDao.login_Owner(con, loginName, pass);
            //2.进行判断，作出响应
            if (owner != null){
                if (owner.getStatus() != 0){
                    notification.setFlag(true);//设置标志位为true
                    //将登录成功的对象保存在map中
                    notification.getMap().put("nowOwner",owner);
                    //这里放一个o_id，用来处理登录后的操作
                    notification.getMap().put("o_id",owner.getOwner_id());
                    notification.getMap().put("name",owner.getName());
                }else {
                    notification.setFlag(false);
                    notification.getMap().put("message","该主人已注销，请重新操作！");
                }
            }else{
                notification.setFlag(false);
                notification.getMap().put("message","账户或密码不正确，请重新操作！");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        //返回结果
        return notification;
    }
    //注册
    public Notification registerOwner(Notification notification){
        //提供数据库链接
        Connection con = DataSourcesTool.getConnection();
        //获取用户名和密码
        Owner newOwner = (Owner) notification.getMap().get("newOwner");

        try {
            //1.调用数据库的操作方法来进行登录校验
            boolean insert_owner = ownerDao.insert_Owner(con, newOwner);
            //2.进行判断，作出响应
            if (insert_owner){
                notification.setFlag(true);//设置标志位为true
                //将登录成功的对象保存在map中
                notification.getMap().put("newOwner",newOwner);
//                notification.getMap().put("o_id",owner.getOwner_id());
            }else{
                notification.setFlag(false);
                notification.getMap().put("message","注册失败，请重新操作！");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        //返回结果
        return notification;
    }
    // 查询已有宠物 --查询pet表，store_id为null的宠物
    public Notification checkPet(Notification notification) {
        Connection con = DataSourcesTool.getConnection();
        int o_id = (int) notification.getMap().get("o_id");
        try {
            List<Pet> pets = petDao.select_PetByOwner_id(con,o_id);
            if (pets!=null && pets.size()!=0){
                notification.setFlag(true);//设置标志位为true
                notification.getMap().put("my-list",pets);
            }else{
                notification.setFlag(false);
                notification.getMap().put("message","抱歉，你没有宠物");
            }

        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }
    // 查询个人信息
    public Notification checkOwner(Notification notification) {
        Connection con = DataSourcesTool.getConnection();
        int o_id = (int) notification.getMap().get("o_id");
        try {
            Owner owner = ownerDao.select_OwnerByID(con,o_id);
            if (owner!=null){
                notification.setFlag(true);//设置标志位为true
                notification.getMap().put("my-info",owner);
            }else{
                notification.setFlag(false);
                notification.getMap().put("message","抱歉，查询失败");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }
    // 修改商店名字
    public Notification updateOwnerName(Notification notification) {
        Connection con = DataSourcesTool.getConnection();
        int o_id = (int) notification.getMap().get("o_id");
        String name = (String) notification.getMap().get("name");
        try {
            boolean res = ownerDao.update_OwnerName(con, o_id, name);
            if (res){
                notification.setFlag(true);//设置标志位为true
                notification.getMap().put("success","更新成功");
            }else{
                notification.setFlag(false);
                notification.getMap().put("message","抱歉，更新失败");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }
    // 修改商店密码
    public Notification updateOwnerPw(Notification notification) {
        Connection con = DataSourcesTool.getConnection();
        int o_id = (int) notification.getMap().get("o_id");
        String pw = (String) notification.getMap().get("pw");
        try {
            boolean res = ownerDao.update_OwnerPw(con,o_id,pw);
            if (res){
                notification.setFlag(true);//设置标志位为true
                notification.getMap().put("success","更新成功");
            }else{
                notification.setFlag(false);
                notification.getMap().put("message","抱歉，更新失败");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }
    // 修改商店财富
    public Notification updateOwnerWea(Notification notification) {
        Connection con = DataSourcesTool.getConnection();
        int o_id = (int) notification.getMap().get("o_id");
        double wea = (double) notification.getMap().get("wea");
        try {
            boolean res = ownerDao.update_OwnerWea(con, o_id, wea);
            if (res){
                notification.setFlag(true);//设置标志位为true
                notification.getMap().put("success","更新成功");
            }else{
                notification.setFlag(false);
                notification.getMap().put("message","抱歉，更新失败");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }
    //     查询交易记录
    public Notification checkAccount(Notification notification) {
        Connection con = DataSourcesTool.getConnection();
        int o_id = (int) notification.getMap().get("o_id");
        try {
            List<Account> accounts = accountDao.selectAccountByOwner(con,o_id);
            if (accounts!=null && accounts.size()!=0){
                notification.setFlag(true);//设置标志位为true
                notification.getMap().put("account-list",accounts);
            }else{
                notification.setFlag(false);
                notification.getMap().put("message","抱歉，你还没有交易记录");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }
    //     注销主人 账号
    public Notification deleteOwner(Notification notification) {
        Connection con = DataSourcesTool.getConnection();
        int o_id = (int) notification.getMap().get("o_id");
        try {
            boolean res = ownerDao.delete_Owner(con, o_id);
            if (res){
                notification.setFlag(true);//设置标志位为true
                notification.getMap().put("success","账号注销成功");
            }else{
                notification.setFlag(false);
                notification.getMap().put("message","抱歉，账号注销失败");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }
    //     查询出所有库存宠物
    public Notification checkAllPet(Notification notification) {
        Connection con = DataSourcesTool.getConnection();
        try {
            List<Pet> pets = petDao.select_AllPet(con);
            if (pets!=null && pets.size()!=0){
                notification.setFlag(true);//设置标志位为true
                notification.getMap().put("pet-list",pets);
            }else{
                notification.setFlag(false);
                notification.getMap().put("message","抱歉，没有待售宠物了");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }
    //     查询出所有商店
    public Notification checkAllPetstore(Notification notification) {
        Connection con = DataSourcesTool.getConnection();
        try {
            List<Petstore> petstores = petStoreDao.selectAllStore(con);
            if (petstores!=null && petstores.size()!=0){
                notification.setFlag(true);//设置标志位为true
                notification.getMap().put("petstore-list",petstores);
            }else{
                notification.setFlag(false);
                notification.getMap().put("message","抱歉，没有可供回收的宠物商店了");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }
    //购买宠物
    public Notification ownerBuyPet(Notification notification) {
        Connection con = DataSourcesTool.getConnection();
        int o_id = (int) notification.getMap().get("o_id");
        int p_id = (int) notification.getMap().get("p_id");
        try {
            //关闭自动提交
            con.setAutoCommit(false);
            // (1)扣除宠物主人金额【宠物的金额】
            // --根据p_id把宠物对象查询出来
            Pet pet = petDao.select_PetByID(con, p_id);
            //先把owner查出来，方便下判断价钱
            Owner owner = ownerDao.select_OwnerByID(con, o_id);
            //如果商店id为空，则说明没有该宠物
            if (pet.getStore_id() == null || pet.getStore_id() == 0) {
                throw new SQLException("该宠物无法购买");
            }
            if (owner.getWealth()-pet.getWorth()<0) {
                throw new SQLException("不够钱买该宠物啦");
            }
            // --根据owner_id修改主人的金额,扣除金额
            ownerDao.update_OwnerWealth1(con, o_id, pet.getWorth());

            // (2)添加宠物商店余额
            petStoreDao.update_PetstoreWealth1(con, pet.getStore_id(), pet.getWorth());
            // (3)添加帐户表一条记录(account表) type=1表示主人买宠物
            Account account = new Account(pet.getStore_id(), o_id, 1, null, pet.getWorth(), pet.getPet_id());

            accountDao.insert_Account(con, account);
            // (4)更新宠物表，store_id设为null,owner_id是主人ID
            boolean res = petDao.updatePetOwner(con, null, o_id, p_id);

            if (res){
                notification.setFlag(true);//设置标志位为true
                notification.getMap().put("success","购买成功");
            }else{
                notification.setFlag(false);
                notification.getMap().put("message","抱歉，购买失败");
            }
        } catch (SQLException sqlException) {
            try {
                con.rollback();
                System.out.println("回滚");
            } catch (SQLException sqlException1) {
                sqlException1.printStackTrace();
            }
            sqlException.printStackTrace();
        }
        try {
            con.commit();
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }
    //出售宠物
    public Notification ownerSellPet(Notification notification) {
        Connection con = DataSourcesTool.getConnection();
        int o_id = (int) notification.getMap().get("o_id");
        int p_id = (int) notification.getMap().get("p_id");
        int s_id = (int) notification.getMap().get("s_id");
        try {
            //关闭自动提交
            con.setAutoCommit(false);
             // (1)扣除宠物主人金额【宠物的金额】
            // --根据p_id把宠物对象查询出来
            Pet pet = petDao.select_PetByID(con, p_id);
            //先把petstore查出来，方便下判断价钱
            Petstore petstore = petStoreDao.select_PetstoreByID(con, s_id);
            //如果商店id为空，则说明没有该宠物
            if (pet.getOwner_id() == null || pet.getOwner_id() == 0) {
                throw new SQLException("该宠物无法出售");
            }
            if (petstore.getWealth()-pet.getWorth()<0) {
                throw new SQLException("该商店不够钱买这个宠物了");
            }
            // --根据owner_id修改主人的金额,添加金额
            ownerDao.update_OwnerWealth2(con, o_id, pet.getWorth());

            // (2)扣除宠物商店余额
            petStoreDao.update_PetstoreWealth2(con, s_id, pet.getWorth());
            // (3)添加帐户表一条记录(account表) type=2表示主人卖宠物
            Account account = new Account(pet.getOwner_id(), s_id, 2, null, pet.getWorth(), pet.getPet_id());

            accountDao.insert_Account(con, account);
            // (4)更新宠物表，store_id设为null,owner_id是主人ID
            boolean res = petDao.updatePetOwner(con, s_id, null, p_id);

            if (res){
                notification.setFlag(true);//设置标志位为true
                notification.getMap().put("success","出售成功");
            }else{
                notification.setFlag(false);
                notification.getMap().put("message","抱歉，出售失败");
            }
        } catch (SQLException sqlException) {
            try {
                con.rollback();
                System.out.println("回滚");
            } catch (SQLException sqlException1) {
                sqlException1.printStackTrace();
            }
            sqlException.printStackTrace();
        }
        try {
            con.commit();
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }
}
